﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ThiefRun.Environment
{
    public class Tile
    {
        #region Constants
        // Constants
        #endregion Constants

        #region Properties
        // Properties
        private Vector2 left;
        private Texture2D background;
        private Vector2 size;
        public enum Passability { Passable, Impassable };
        private Passability passability;
        private int levelChange;
        #endregion Properties

        #region Computed Properties
        // Computed Properties
        public Vector2 Left { get { return left; } }
        public Vector2 Size { get { return size; } }
        public Rectangle Bounds { get { return new Rectangle((int)(left.X), (int)(left.Y), (int)size.X, (int)size.Y); } }
        public BoundingBox BoundingBox
        { 
            get 
            {
                return new BoundingBox(new Vector3(left.X - (size.X / 2F), left.Y - (size.Y / 2F), 0F),
                                       new Vector3(left.X + (size.X / 2F), left.Y + (size.Y / 2F), 0F));
            }
        }
        public int LevelChange { get { return levelChange; } }
        public Vector2 Middle { get { return new Vector2(left.X + (size.X / 2F), left.Y + (size.Y / 2F)); } }
        #endregion

        #region Methods
        // Methods

        /// <summary>
        /// Initialize the tile object.
        /// </summary>
        /// <param name="center">Provides the coordinates of the center of the tile.</param>
        /// <param name="size">Provides the components of the size of the tile.</param>
        public Tile(Vector2 center, Vector2 size)
        {
            this.left = center;
            this.size = size;
            this.passability = Passability.Passable;
            levelChange = 0;
        }

        /// <summary>
        /// Initialize the tile object.
        /// </summary>
        /// <param name="center">Provides the coordinates of the center of the tile.</param>
        /// <param name="size">Provides the components of the size of the tile.</param>
        /// <param name="background">Provides the texture displayed on the tile.</param>
        public Tile(Vector2 center, Vector2 size, Texture2D background, Passability passability, int levelChange)
        {
            this.left = center;
            this.size = size;
            this.background = background;
            this.passability = passability;
            this.levelChange = levelChange;
        }

        /// <summary>
        /// Draw the tile on the screen.
        /// </summary>
        /// <param name="gameTime">Provides access to the timer.</param>
        /// <param name="spriteBatch">Provides an object to a sprite.</param>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
                spriteBatch.Draw(background, Bounds, Color.White);
            spriteBatch.End();
        }

        /// <summary>
        /// A character collides with the tile
        /// </summary>
        /// <param name="gameTime">Provides access to the timer.</param>
        /// <param name="spriteBatch">Provides an object to a sprite.</param>
        public bool Collides(ThiefRun.Character.Character character)
        {
            return (Bounds.Intersects(character.Bounds) && character.OnCurrentFloor);
        }
        #endregion Methods
    }
}
